package com.squirrel.common.service.impl;

import com.squirrel.common.entity.User;
import com.squirrel.common.entity.UserSubscription;
import com.squirrel.common.exception.BaseException;
import com.squirrel.common.mapper.UserSubscriptionMapper;
import com.squirrel.common.param.userVideo.OperationParam;
import com.squirrel.common.response.code.CodeEnum;
import com.squirrel.common.service.UserService;
import com.squirrel.common.service.UserSubscriptionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.squirrel.common.vo.userSubscription.UpVO;
import com.squirrel.common.vo.userSubscription.UserSubBaseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * UserSubscriptionServiceImp 用户关注服务实现类
 * </p>
 *
 * @author 关远键
 * @since 2021/11/11 16:06
 */
@Service
public class UserSubscriptionServiceImp extends ServiceImpl<UserSubscriptionMapper, UserSubscription> implements UserSubscriptionService {

    private static final Integer IDOL = 1;
    private static final Integer FANS = 2;

    @Autowired
    private UserSubscriptionMapper usMapper;

    @Autowired
    private UserService userService;

    @Value("${file.coming.path}")
    private String url;

    /**
     * 偶像列表
     *
     * @param userId 用户ID
     * @return com.squirrel.common.vo.userSubscription.FromIdVO
     * @author 关远键
     * @date 2021/11/22 10:25
     */
    @Override
    public UpVO getIdols(Integer userId) {
        List<UserSubscription> usList = usMapper.getByFromId(userId);

        ArrayList<UserSubBaseVO> list = formatList(usList, IDOL);

        return new UpVO().setId(userId).setList(list);
    }

    /**
     * 粉丝列表
     * @author 关远键
     * @date 2021/11/22 10:26
     * @param userId 用户ID
     * @return com.squirrel.common.vo.userSubscription.FromIdVO
     */
    @Override
    public UpVO getFans(Integer userId) {
        List<UserSubscription> usList = usMapper.getByToId(userId);

        ArrayList<UserSubBaseVO> list = formatList(usList, FANS);

        return new UpVO().setId(userId).setList(list);
    }

    /**
     * 添加关注
     * @author 关远键
     * @date 2021/11/22 10:44
     * @param param
     * @return boolean
     */
    @Override
    public boolean add(OperationParam param) {
        UserSubscription userSubscription = new UserSubscription();
        userSubscription.setFromUserId(param.getFromId());
        userSubscription.setToUserId(param.getToId());
        userSubscription.setSubscriptionTime(LocalDateTime.of(LocalDate.now(), LocalTime.now()));

        if (0 < usMapper.isExist(param.getFromId(), param.getToId())) {
            throw new BaseException(CodeEnum.ERROR);
        }

        if (0 < usMapper.insert(userSubscription)) {
            return true;
        }

        throw new BaseException(CodeEnum.DB_EXCEPTION);
    }

    /**
     * 取消关注
     * @author 关远键
     * @date 2021/11/22 10:44
     * @param param
     * @return boolean
     */
    @Override
    public boolean delete(OperationParam param) {
        if (isExist(param)) {
            Integer i = usMapper.deleteSub(param.getFromId(), param.getToId());
            if (0 < i) {
                return true;
            }
        }
        throw new BaseException(CodeEnum.DB_EXCEPTION);
    }

    /**
     * 是否已关注
     * @author 关远键
     * @date 2021/11/23 22:39
     * @param param
     * @return boolean
     */
    @Override
    public boolean isExist(OperationParam param) {
        if (0 == usMapper.isExist(param.getFromId(), param.getToId())) {
//            throw new BaseException(CodeEnum.ERROR);
            return false;
        }
        return true;
    }

    /**
     * 格式化返回（逻辑内聚）
     * @author 关远键
     * @date 2021/11/22 10:37
     * @param usList 列表
     * @param type 偶像或粉丝
     * @return java.util.ArrayList<com.squirrel.common.vo.userSubscription.UserSubBaseVO>
     */
    private ArrayList<UserSubBaseVO> formatList(List<UserSubscription> usList, Integer type) {
        if (null == usList) {
            throw new BaseException(CodeEnum.INVALID_RESOURCE);
        }

        ArrayList<UserSubBaseVO> list = new ArrayList<>();
        for (UserSubscription us: usList) {
            User user = null;

            if (Objects.equals(IDOL, type)) {
                user = userService.getUserById(us.getToUserId());
            } else {
                user = userService.getUserById(us.getFromUserId());
            }

            UserSubBaseVO tmp = new UserSubBaseVO();
            tmp.setId(user.getId());
            tmp.setAvatar(url + user.getAvatar());
            tmp.setNickname(user.getNickname());

            list.add(tmp);
        }

        return list;
    }

}
